﻿Public Class Frm_District
    Implements IDServer.IDServer.IDServer

    Private WithEvents MainForm As Frm_MainForm
    Private mDataset As DataSet
    Private SelectNode As TreeNode

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#Region "Sub And Function"


    Private Sub ClearControl()
        Try
            Me.TxtAreaID.Text = ""
            Me.TxtAreaName.Text = ""
            Me.TxtAreaOrg.Text = ""
            Me.TxtAreaRemark.Text = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ControlEnable(ByVal B As Boolean)
        Try
            Me.TxtAreaID.Enabled = B
            Me.TxtAreaName.Enabled = B
            Me.TxtAreaOrg.Enabled = B
            Me.TxtAreaRemark.Enabled = B

        Catch ex As Exception

        End Try

    End Sub

    Private Sub ActionEnable(ByVal B As Boolean)
        Try
            If EditEnable = False Then
                MessageBox.Show(My.Resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            With Me.MainForm
                .ToolStripCancel.Enabled = B
                .ToolStripSave.Enabled = B
                .ToolStripEdit.Enabled = B
                .ToolStripCheck.Enabled = B
                .ToolStripUnCheck.Enabled = B
                .ToolStripExist.Enabled = True
                .ToolStripExport.Enabled = B
                .ToolStripDelete.Enabled = B
                .ToolStripFilter.Enabled = B
                .ToolStripImport.Enabled = B
                .ToolStripAdd.Enabled = B
                .ToolStripComplete.Enabled = B
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Function ConditonToSave() As Boolean
        Try
            Dim B As Boolean = True
            If Me.TxtAreaID.Text = "" Then
                MessageBox.Show("區域編號為空！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
                B = False
            End If
            If Me.TxtAreaName.Text = "" Then
                MessageBox.Show("區域名稱為空", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
                B = False
            End If
            If Me.TxtAreaOrg.Text = "" Then
                MessageBox.Show("區域序號為空", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
                B = False
            End If
            Return B
        Catch ex As Exception

        End Try
    End Function

    Private Sub Frm_LoTransport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim B As Boolean = False
            ControlEnable(B)
            ActionEnable(B)
            LoadTreeView(TreeViewArea)

        Catch ex As Exception

        End Try
    End Sub

    Private Sub LoadTreeView(ByVal TV As TreeView)
        Try
            Dim mNode As New TreeNode
            Dim SQLstring As String = ""

            TV.Nodes.Clear()
            With mNode
                .Tag = "all"
                .Text = "全部"
            End With
            TV.Nodes.Add(mNode)
            SQLstring = "select * from dbo.LO_AreaSetup"

            mDataset = New DataSet
            mDataset = ReturnDataset(SQLstring)

            LoadSecondeTreeView(mNode, 0)
            SelectNode = mNode
            mNode.Expand()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub LoadSecondeTreeView(ByVal mNode As TreeNode, ByVal fAreaOrg As String)
        Try
            If mDataset Is Nothing Then Exit Sub

            Dim mrows() As DataRow = mDataset.Tables(0).Select("fAreaOrg='" & fAreaOrg & "'")
            For Each row As DataRow In mrows
                Dim mNewNode As New TreeNode
                If mNode Is Nothing Then
                    With mNewNode
                        .Tag = row("fAreaID")
                        .Text = row("fAreaName")
                    End With
                    mNode.Nodes.Add(mNewNode)
                    LoadSecondeTreeView(mNewNode, row("fAreaID"))
                Else
                    With mNewNode
                        .Tag = row("fAreaID")
                        .Text = row("fAreaName")
                    End With
                    mNode.Nodes.Add(mNewNode)
                    LoadSecondeTreeView(mNewNode, row("fAreaID"))

                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub TreeViewArea_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeViewArea.AfterSelect
        Try

            Dim B As Boolean
            If SelectNode Is Nothing Then Exit Sub
            SelectNode = e.Node
            ControlEnable(B)
            FillText(SelectNode.Tag.ToString)

            MainForm.ToolStripEdit.Enabled = True
            MainForm.ToolStripDelete.Enabled = True

        Catch ex As Exception

        End Try
    End Sub

    Private Sub FillText(ByVal fAreaID As String)
        Try
            Dim rows() As DataRow = mDataset.Tables(0).Select("fAreaID='" & fAreaID & "'")
            If rows.Length > 0 Then
                For Each row As DataRow In rows
                    Me.TxtAreaID.Text = row("fAreaID")
                    Me.TxtAreaName.Text = row("fAreaName")
                    Me.TxtAreaOrg.Text = row("fAreaOrg")
                    Me.TxtAreaRemark.Text = row("fAreaRemark")
                Next
            End If

        Catch ex As Exception
        End Try
    End Sub

    Private Function AreaIsExits() As Boolean
        Try
            Dim fAreaID As String = Me.TxtAreaID.Text
            Dim SQL As String = ""
            Dim RST As DataSet
            Dim B As Boolean = True

            SQL = "select * from dbo.LO_AreaSetup where fAreaID='" & fAreaID & "'"
            RST = New DataSet
            RST = ReturnDataset(SQL)

            If RST.Tables(0).Rows.Count > 0 Then
                B = False
            Else
                B = True
            End If
            Return B
        Catch ex As Exception

        End Try

    End Function


#End Region

#Region "Interface Define"
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

        Try
            If AddEnable = False Then
                MessageBox.Show(My.Resources.NotAllowAdd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Dim B As Boolean = True
            ControlEnable(B)
            ClearControl()
            With Me.MainForm
                .ToolStripCancel.Enabled = B
                .ToolStripSave.Enabled = B
                .ToolStripEdit.Enabled = Not B
                .ToolStripCheck.Enabled = Not B
                .ToolStripUnCheck.Enabled = Not B
                .ToolStripExist.Enabled = True
                .ToolStripExport.Enabled = Not B
                .ToolStripDelete.Enabled = Not B
                .ToolStripFilter.Enabled = Not B
                .ToolStripImport.Enabled = Not B
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            With MainForm
                .ToolStripAdd.Enabled = True
                .ToolStripSave.Enabled = False
                .ToolStripEdit.Enabled = False
                .ToolStripCancel.Enabled = False
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.Resources.NotAllowDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If
            If SelectNode Is Nothing Then Exit Function
            If MessageBox.Show("您確定要刪除此區域設置嗎？", Msg.CompanyName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                Dim fAreaID As String = SelectNode.Tag.ToString.Trim
                Dim strSQL As String = ""
                strSQL = "delete LO_AreaSetup where fAreaID='" & fAreaID & "'"
                ReturnObject(strSQL)
                MessageBox.Show(My.Resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.TreeViewArea.Nodes.Remove(SelectNode)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show(My.Resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If
            Dim B As Boolean = True
            If Not SelectNode Is Nothing Then
                With Me.MainForm
                    .ToolStripCancel.Enabled = B
                    .ToolStripSave.Enabled = B
                    .ToolStripEdit.Enabled = Not B
                    .ToolStripCheck.Enabled = B
                    .ToolStripUnCheck.Enabled = B
                    .ToolStripExist.Enabled = True
                    .ToolStripExport.Enabled = Not B
                    .ToolStripDelete.Enabled = Not B
                    .ToolStripFilter.Enabled = Not B
                    .ToolStripImport.Enabled = Not B
                End With
            End If
            ControlEnable(B)
            Me.TxtAreaID.Enabled = Not B
        Catch ex As Exception
            MessageBox.Show(ex.ToString, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Try
            If ConditonToSave() = False Then
                Exit Function
            End If
            Dim fAreaID As String = Me.TxtAreaID.Text
            Dim fAreaName As String = Me.TxtAreaName.Text
            Dim fAreaOrg As String = Me.TxtAreaOrg.Text
            Dim fAreaRemark As String = Me.TxtAreaRemark.Text
            Dim B As Boolean = False
            Dim SQL As String = ""


            If AreaIsExits() = True Then
                SQL = "insert into LO_AreaSetup(fAreaID,fAreaName,fAreaOrg,fAreaRemark) values('" & fAreaID & "','" & fAreaName & "','" & fAreaOrg & "','" & fAreaRemark & "')"
            Else
                SQL = "update LO_AreaSetup set fAreaName='" & fAreaName & "',fAreaOrg='" & fAreaOrg & "',fAreaRemark='" & fAreaRemark & "' where fAreaID='" & fAreaID & "'"
            End If

            ReturnObject(SQL)
            MessageBox.Show("設置完成!", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)

            ControlEnable(B)


        Catch ex As Exception

        End Try
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function
#End Region


    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click

    End Sub
End Class
